package com.trsj.lnsj.quartz.job;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.trsj.lnsj.controller.ActivityController;
import com.trsj.lnsj.entity.OrderDTO;
import com.trsj.lnsj.mapper.ActivityMapper;
import com.trsj.lnsj.mapper.OrderMapper;
import com.trsj.lnsj.mapper.SignupRecordMapper;

/**  

* <p>Title: CleanExpiredOrder.java</p>  

* <p>Description: </p>  

* <p>Copyright: Copyright (c) 2018</p>  

* <p>Company: 七色环</p>  

* @author longky  

* @date 2018年9月12日  

* @version 1.0  

*/

@Component
public class CleanExpiredOrderJob {
	   private Logger logger = LoggerFactory.getLogger(CleanExpiredOrderJob.class);
	  @Autowired
	   private OrderMapper orderMaper;
	  @Autowired
	   private ActivityMapper activityMapper;
	  @Autowired
	   private SignupRecordMapper signupRecordMapper;
	  @Scheduled(fixedRate = 100000)
	public void doSomething(){
		  LocalDateTime now = LocalDateTime.now();
		  LocalDateTime expiredTime=   now.minusMinutes(30);
		  Long time=expiredTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
		  Timestamp scurrtest = new Timestamp(time);
		  List<OrderDTO> list=orderMaper.selectExpiredOrder(scurrtest);
		  if(list==null)
			  return;
		  logger.info("清理过期订单："+new Date()+"   过期订单数量："+list.size()+"  time: " + scurrtest);
		  for(OrderDTO order:list) {
			int result=  signupRecordMapper.setInvalidStatusByOrderId(order.getOrderId());
			orderMaper.updateOrderStatusById(order.getOrderId(),-3);
			activityMapper.unSignup(order.getActivityId(),order.getPersonNum());
		  }
		 
		    }


}

